//引入express
const express=require('express');
//引入路由器
const userRouter = require('./router/user.js');
const worksRouter = require('./router/works.js');
const LaptopClassRouter = require('./router/Laptop_class.js');
const ClassHotRouter = require('./router/Class_hot.js');
const ClassFashionRouter = require('./router/Class_fashion.js');
const ClassCuisineRouter = require('./router/Class_cuisine.js');
const SearchRouter = require('./router/search.js');
//引入body-parser中间件模块
const bodyParser=require('body-parser');
//创建WEB服务器
const app=express();
//设置端口
app.listen(3000);
console.log("server is runing ... 3000")

//引入跨域管理模块
const cors = require("cors");
// //配置允许跨域程序
app.use(cors({
	 origin:["http://127.0.0.1:8080","http://localhost:8080"]
}));


//托管静态资源到public目录
app.use( express.static('public') );
//使用body-parser中间件，将post请求的数据解析为对象
app.use(bodyParser.urlencoded({
  extended:false
}));
  //添加错误处理中间件，拦截所有产生的错误
app.use((err,req,res,next)=>{
	console.log(err);
	res.send({code:500,msg:'服务器端错误'});
});
//挂载（）路由器，添加前缀（）
app.use('/user',userRouter);
app.use('/works',worksRouter);
app.use('/class',LaptopClassRouter);
app.use('/class/hot',ClassHotRouter);	
app.use('/class/fashion',ClassFashionRouter);
app.use('/class/cuisine',ClassCuisineRouter);
app.use('/search',SearchRouter)